package com.firsteapps.mobile.android.puzzle;

import java.io.File;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.firsteapps.mobile.android.puzzle.game.Helper;
import com.firsteapps.mobile.android.puzzle.util.Consts;
import com.firsteapps.mobile.android.puzzle.util.Utilities;

public class CreateProfile extends BaseActivity {

	EditText fieldUserName, fieldFirstName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_profile);

		fieldFirstName = (EditText) findViewById(R.id.cp_first_name);
		fieldUserName = (EditText) findViewById(R.id.cp_username);

		final File usersDir = new File(getFilesDir() + "/" + Consts.USERS_FOLDER);
		usersDir.mkdir();

		findViewById(R.id.crpr_cancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Utilities.startActivityWithAnimation(CreateProfile.this, SelectProfile.class);
			}
		});

		findViewById(R.id.crpr_submit).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String userName = null, firstName = null;
				boolean validCredentials = false;
				if (fieldFirstName.getText() != null && fieldUserName.getText() != null) {
					firstName = fieldFirstName.getText().toString();
					userName = fieldUserName.getText().toString();
					if (firstName.length() > 0 && !firstName.contains(Consts.FILENAME_SPLITTER) && userName.length() > 0
							&& !userName.contains(Consts.FILENAME_SPLITTER)) {
						String[] users = usersDir.list();
						validCredentials = true;
						for (int i = 0; i < users.length; i++) {
							if (users[i].contains(userName + Consts.FILENAME_SPLITTER)) {
								validCredentials = false;
								Toast.makeText(CreateProfile.this, "The username " + userName + " already exists.",
										Toast.LENGTH_LONG).show();
								return;
							}
						}
					}
				}

				if (validCredentials) {
					final File createUser = new File(usersDir, userName + Consts.FILENAME_SPLITTER + firstName);
					Helper.doMessageDialog(CreateProfile.this, "Create Profile", "Are you sure you want to create the profile?",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									if (createUser.mkdir()) {
										File completedPuzzles = new File(createUser, Consts.FOLDER_NAME_COMPLETED_PUZZLES.substring(1));
										Helper.createConfigFile(CreateProfile.this, completedPuzzles);
										File incompletePuzzles = new File(createUser, Consts.FOLDER_NAME_INCOMPLETE_PUZZLES.substring(1));
										Helper.createConfigFile(CreateProfile.this, incompletePuzzles);
										Utilities.startActivityWithAnimation(CreateProfile.this, SelectProfile.class);
									} else
										Toast.makeText(CreateProfile.this, "Unable to create the profile.", Toast.LENGTH_LONG)
												.show();
								}
							}, true);
				} else
					Toast.makeText(CreateProfile.this, "Incorrect username or firstname", Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);

		findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Utilities.startActivityWithAnimation(CreateProfile.this, SelectProfile.class);
			}
		});
	}
}
